package tacticalLayer.tactics;

import org.jdom.Element;

import planninglanguage.plans.AttackBOPlan;
import planninglanguage.plans.BasicOperatorPlan;
import planninglanguage.plans.Plan;
import util.XMLWriter;
import base.WargusGameState;

/*
 * This tactic simply executes the basic operation and ends
 */

public class DefaultTactic extends Tactic {	
	BasicOperatorPlan m_goal;
	
	public DefaultTactic(int unitID,BasicOperatorPlan goal) {
		super(unitID);
		m_goal = goal;
	}
	
	/*
	 * Returns "true" when the Tactic has finished 
	 */
	public Tactic cycle(WargusGameState gameState) {
		m_goal.send();
		return null;
	}
	
	public void writeXML(XMLWriter w) {
		w.tag("tactic type=\"default\"");
		{
			w.tag("unit-id", m_unitID);
			w.tag("plan");
			{
				m_goal.writeXML(w);
			}
			w.tag("/plan");
		}
		w.tag("/tactic");
	}
	
	protected static DefaultTactic loadTacticFromXML(Element e) {
		assert e.getName().equals("tactic") :
			"DefaultTactic.loadFromXML: Invalid XML Element " +	e.getName();
		assert e.getAttributeValue("type").equals("default") :
			"DefaultTactic.loadFromXML: Invalid Tactic type " +	
			e.getAttributeValue("type");
		
		int unitID = Integer.parseInt(e.getChildText("unit-id"));
		AttackBOPlan plan = (AttackBOPlan) Plan.loadPlanFromXML(e.getChild("plan")); 
		
		return new DefaultTactic(unitID, plan);
	}
}
